﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MenusHunter.WinRT.Core.MVVM;
using MenusHunter.WinRT.Core.Models;
using MenusHunter.WinRT.Core.Services.Account;
using Windows.UI.Xaml;

namespace MenusHunter.WinRT.Core.ViewModels
{
    public class LoginPageViewModel : ViewModelBase
    {
        private string _email;
        private string _password;
        private string _loginResult;
        private ReplayCommand _loginCommand;
        private string _avatar;

        #region Properties

        public string Email
        {
            get { return _email; }
            set { SetProperty(ref _email, value); }
        }

        public string Password
        {
            get { return _password; }
            set { SetProperty(ref _password, value); }
        }

        public string LoginResult
        {
            get { return _loginResult; }
            set { SetProperty(ref _loginResult, value); }
        }

        public string Avatar
        {
            get { return _avatar; }
            set { SetProperty(ref _avatar, value); }
        }

        #endregion

        #region Commands

        public ReplayCommand LoginCommand
        {
            get
            {
                if (_loginCommand == null)
                {
                    _loginCommand = new ReplayCommand(async param =>await Login(), param => !IsBusy);
                }
                return _loginCommand;
            }
            set { SetProperty(ref _loginCommand, value); }
        }

        #endregion

        #region Methods

        private async Task Login()
        {
            if (string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(Password)) return;
            IsBusy = true;            
            var acc = await AccountsManager.CheckLoginAsync(Email, Password);

            if (acc != null && acc.Id > 0)
            {
                LoginResult = "Login Success!";
                IsBusy = false;
                OnLoginSuccessed(new LoginEventHandlerArgs{Account = acc,Email = Email,Password = Password});
            }
            else
            {
                LoginResult = "Login Failed";
                IsBusy = false;
                OnLoginFailed(new LoginEventHandlerArgs{Email = Email,Password = Password,Account = null});
            }
        }

        #endregion

        #region Events

        public event LoginEventHandler LoginSuccessed;

        public void OnLoginSuccessed(LoginEventHandlerArgs args)
        {
            var handler = LoginSuccessed;
            if (handler != null) handler(this, args);
        }

        public event LoginEventHandler LoginFailed;

        public void OnLoginFailed(LoginEventHandlerArgs args)
        {
            var handler = LoginFailed;
            if (handler != null) handler(this, args);
        }

        #endregion
    }

    public delegate void LoginEventHandler(object sender, LoginEventHandlerArgs args);

    public class LoginEventHandlerArgs
    {
        public Account Account { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
    }
}